package article

import (
	"gin-project-01/gin-step04/gin16-multi-tables/controllers"
	myModel "gin-project-01/gin-step04/gin16-multi-tables/models"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ArticleController struct {
	controllers.BaseController //继承 controllers.BaseController
}

// ArtiList
//
//	@Description:  http://localhost:8888/arti/list/0 | http://localhost:8888/arti/list/1
//	@receiver a
//	@param c
func (a ArticleController) ArtiList(c *gin.Context) {
	all := c.Param("all")
	if all == "1" {
		var articleDtoList []myModel.ArticleDto
		//查询所有文章及对应的分类，"ArticleCate"---对应ArticleDto.ArticleCate
		myModel.Db.Order("id asc").Preload("ArticleCate").Find(&articleDtoList)
		c.JSON(http.StatusOK, myModel.Success("", articleDtoList))
	} else {
		var articleList []myModel.Article
		//查询所有文章
		myModel.Db.Order("id asc").Find(&articleList)
		c.JSON(http.StatusOK, myModel.Success("", articleList))
	}
}

// CateList
//
//	@Description: http://localhost:8888/cate/list/0 | http://localhost:8888/cate/list/1
//	@receiver a
//	@param c
func (a ArticleController) CateList(c *gin.Context) {
	all := c.Param("all")
	if all == "1" {
		var articleCateDtoList []myModel.ArticleCateDto
		//查询所有分类及以下的文章s，"ArticleList"---对应ArticleCateDto.ArticleList
		myModel.Db.Preload("ArticleList").Find(&articleCateDtoList)
		c.JSON(http.StatusOK, myModel.Success("", articleCateDtoList))
	} else {
		var articleCateList []myModel.ArticleCate
		myModel.Db.Find(&articleCateList)
		c.JSON(http.StatusOK, myModel.Success("", articleCateList))
	}

}
